﻿using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using eStock.ChildWindows;
using eStock.DataObjects;

namespace eStock.Controls
{
    partial class UIComponents
    {
        private void LbItemsSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Component c = lbItems.SelectedItem as Component;
            if (c == null) return;
            txtTitle.Text = c.Title;
            txtDescription.Text = c.Description;
            if (c.Type != null)
            {
                txtType.Value = c.Type.Title;
                txtCategory.Value = c.Type.Category.Title;
            }
            Tray stock =
                DatabaseObjects.Trays.Cast<Tray>().FirstOrDefault(cs => cs.ComponentId == c.Id);
            if (stock != null)
            {
                txtNumStock.Value = stock.Amount.ToString(CultureInfo.InvariantCulture);
                txtprice.Value = stock.Price.ToString(CultureInfo.InvariantCulture) + " €";
                //txtBoxStock.Value = stock.Box;

                lbShops.Items.Clear();
                foreach (Address address in stock.Shops)
                {
                    ListBoxItem item = new ListBoxItem { Content = address.Name, Tag = address.Id };
                    lbShops.Items.Add(item);
                }
            }
            else
            {
                txtNumStock.Value = "0";
            }
            if (c.Image != null)
            {
                imgComponent.Source = c.Image.Image.Source;
                imgComponent.Tag = c.Image.Id;
            }
            else
            {
                imgComponent.Source = null;
            }

            if (c.Type != null && c.Type.Image != null)
            {
                imgType.Source = c.Type.Image.Image.Source;
                imgType.Tag = c.Type.Image.Id;
            }


            //Set data for childWindow Type
            if (c.Type != null)
            {
                cldTypeTxtTitle.Value = c.Type.Title;
                cldTypeCbSmd.IsChecked = c.Type.Smd;

                if (c.Type.Category != null) cldTypeTxtCategory.Value = c.Type.Category.Title;

                cldTypeTxtDescription.Text = c.Type.Description;
                cldTypeTxtPins.Value = c.Type.Pins.ToString(CultureInfo.InvariantCulture);
                cldTypeTxtSerialNr.Value = c.Type.SerialNr;
            }
            //Set data for childWindow Categories
            if (c.Type != null && c.Type.Category != null)
            {
                foreach (TreeViewItem item in tvItems.Items)
                {
                    if ((int)item.Tag == c.Type.Category.Id)
                        item.IsSelected = true;
                }
            }
        }

        private void TxtTypeMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (lbItems.SelectedIndex < 0) return;
            cldType.Show();
        }


        private void BtnDetailsTypeClick(object sender, RoutedEventArgs e)
        {
            if (lbItems.SelectedIndex < 0) return;
            cldType.Show();
        }

        private void BtnDetailsCategoryClick(object sender, RoutedEventArgs e)
        {
            if (lbItems.SelectedIndex < 0) return;
            cldCategory.Show();
        }

        private void TvItemsSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (tvItems.SelectedItem == null) return;
            int id = (int)((TreeViewItem)tvItems.SelectedItem).Tag;
            ComponentCategory c = DatabaseObjects.Categories.GetById(id);
            cldCatTxtTitle.Value = c.Title;
            cldCatTxtDescription.Text = c.Description;
        }

        private void ImgComponentMouseUp(object sender, MouseButtonEventArgs e)
        {
            Component c = lbItems.SelectedItem as Component;
            WndImageView imgView = new WndImageView(c.Image);
            imgView.ShowDialog();
        }

        private void ImgTypeMouseUp(object sender, MouseButtonEventArgs e)
        {
            Component c = lbItems.SelectedItem as Component;
            if (c.Type == null || c.Type.Image == null) return;
            WndImageView imgView = new WndImageView(c.Type.Image);
            imgView.ShowDialog();
        }

        private void LbShopsMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (lbShops.SelectedIndex < 0) return;
            int id = (int)((ListBoxItem)lbShops.SelectedItem).Tag;
            Address a = DatabaseObjects.Addresses.GetById(id);
            if (a == null) return;
            if (a.Image != null) cldShopImage.Source = a.Image.Image.Source;
            cldShopTxtBlz.Value = a.Blz;
            cldShopTxtDescription.Text = a.Description;
            cldShopTxtKonto.Value = a.Konto;
            cldShopTxtMail.Value = a.Mail;
            cldShopTxtName.Value = a.Name;
            cldShopTxtPLZLoc.Value = a.PLZ + " " + a.Location;
            cldShopTxtPohne.Value = a.Phone;
            cldShopTxtStreet.Value = a.Street;
            cldShopTxtWeb.Value = a.Web;

            cldShop.Show();
        }

        private void TxtSearchTextChanged(object sender, TextChangedEventArgs e)
        {
            lbItems.Items.Clear();
            string searchTerm = txtSearch.Text.ToLower();

            if (string.IsNullOrEmpty(txtSearch.Text))
            {
                foreach (Component c in DatabaseObjects.Components)
                {
                    lbItems.Items.Add(c);
                }
            }
            else
            {
                foreach (Component c in DatabaseObjects.Components)
                {
                    if (c.Title.ToLower().Contains(searchTerm) ||
                        (c.Type != null && c.Type.Title.ToLower().Contains(searchTerm)))
                        lbItems.Items.Add(c);
                }
            }
        }

        public override void Update()
        {
            lbItems.Items.Clear();
            foreach (Component c in DatabaseObjects.Components)
            {
                lbItems.Items.Add(c);
            }

            tvItems.Items.Clear();
            foreach (ComponentCategory cc in DatabaseObjects.Categories)
            {
                if (cc.ParentId != -1) continue;
                TreeViewItem item = new TreeViewItem { Header = cc.Title, Tag = cc.Id };
                foreach (ComponentCategory child in DatabaseObjects.Categories.GetChilds(cc.Id))
                {
                    TreeViewItem cItem = new TreeViewItem { Header = child.Title, Tag = child.Id };
                    item.Items.Add(cItem);
                }
                tvItems.Items.Add(item);
            }
            if (lbItems.Items.Count > 0)
                lbItems.SelectedIndex = 0;

            imgComponent.Cursor = Cursors.Hand;
            imgType.Cursor = Cursors.Hand;
        }
    }
}
